package com.fishery.refrigeratory.controller;

import com.fishery.entity.Result;
import com.fishery.refrigeratory.entity.Refrigeratory;
import com.fishery.refrigeratory.entity.RefrigeratoryAccessInfo;
import com.fishery.refrigeratory.entity.RefrigeratoryInInfo;
import com.fishery.refrigeratory.service.RefrigeratoryInInfoService;
import com.fishery.refrigeratory.service.RefrigeratoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * @author wgz
 * @since 2021-2-2
 */
@Api(description = "冷库入库信息接口")
@RestController
@CrossOrigin
@RequestMapping("/refrigeratoryInInfo")
public class RefrigeratoryInInfoController {

    @Autowired
    private RefrigeratoryInInfoService refrigeratoryInInfoService;

    @Autowired
    private RefrigeratoryService refrigeratoryService;

    /**
     * 新增入库信息
     * @param refrigeratoryInInfo
     * @return
     */
    @ApiOperation(value = "新增入库信息")
    @PostMapping("/in")
    public Result saveRefrigeratoryInInfo(@RequestBody @ApiParam(value = "入库信息对象", required = true) RefrigeratoryInInfo refrigeratoryInInfo) {
        if (refrigeratoryInInfo == null
                || StringUtils.isEmpty(refrigeratoryInInfo.getId())
                || StringUtils.isEmpty(refrigeratoryInInfo.getRefrigeratoryId())
                || StringUtils.isEmpty(refrigeratoryInInfo.getProductName())
                || StringUtils.isEmpty(refrigeratoryInInfo.getRefrigeratoryInCapacity())
                || StringUtils.isEmpty(refrigeratoryInInfo.getWarehousingPersonId())
                || StringUtils.isEmpty(refrigeratoryInInfo.getProcessingBaseId())
        ) {
            return Result.error();
        } else if (!StringUtils.isEmpty(refrigeratoryInInfo.getRefrigeratoryInUsedCapacity())
                && refrigeratoryInInfo.getRefrigeratoryInUsedCapacity() > refrigeratoryInInfo.getRefrigeratoryInCapacity()) {
            return Result.error("入库的已用容量不得大于入库的总容量");
        }
        return refrigeratoryInInfoService.insertInInfo(refrigeratoryInInfo) != 0? Result.success("新增成功"): Result.error("新增失败");
    }

    /**
     * 根据入库编号id删除入库信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据入库编号id删除入库信息")
    @DeleteMapping("/in/{id}")
    public Result deleteRefrigeratoryInInfoById(@PathVariable(value = "id", required = true) @ApiParam(value = "入库编号id", required = true) String id) {
        if (id == null) {
            return Result.error();
        }
        return refrigeratoryInInfoService.deleteInInfo(id) != 0? Result.success("删除成功"): Result.error("删除失败");
    }

    /**
     * 修改入库信息
     * @param updateRefrigeratoryInInfo
     * @return
     */
    @ApiOperation(value = "修改入库信息")
    @PutMapping("/in")
    public Result updateRefrigeratoryInInfo(@RequestBody @ApiParam(value = "入库信息对象", required = true) RefrigeratoryInInfo updateRefrigeratoryInInfo) {
        if (updateRefrigeratoryInInfo.getRefrigeratoryInUsedCapacity() != null) {
            return Result.error("不能随意修改入库已用容量！");
        }
        if (updateRefrigeratoryInInfo == null
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getId())
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getRefrigeratoryId())
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getProductName())
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getRefrigeratoryInCapacity())
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getWarehousingPersonId())
                || StringUtils.isEmpty(updateRefrigeratoryInInfo.getProcessingBaseId())
        ) {
            return Result.error();
        }
        return refrigeratoryInInfoService.updateInInfo(updateRefrigeratoryInInfo) != 0? Result.success("修改成功"): Result.error("修改失败");

    }

    /**
     * 根据入库编号id查询入库信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据入库编号id查询入库信息")
    @GetMapping("/in/{id}")
    public Result selectRefrigeratoryById(@PathVariable(value = "id", required = true) @ApiParam(value = "入库编号id", required = true) String id) {
        return Result.success("查询成功").data(refrigeratoryInInfoService.getById(id));
    }

    /**
     * RefrigeratoryId
     * @param RefrigeratoryId
     * @return
     */
    @ApiOperation(value = "根据冷库id查询冷库的入库信息")
    @GetMapping("/selectInfoByRefrigeratoryId/{id}")
    public Result selectInfoByRefrigeratoryId(@PathVariable(value = "id", required = true) @ApiParam(value = "冷库id", required = true) String RefrigeratoryId) {
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectInfoBytRefrigeratoryId(RefrigeratoryId));
    }

    /**
     * 根据冷库id分页查询冷库的入库信息
     * @param RefrigeratoryId
     * @return
     */
    @ApiOperation(value = "根据冷库id分页查询冷库的入库信息")
    @GetMapping("/selectPageInfoByRefrigeratoryId/{id}/{page}/{size}")
    public Result selectPageInfoByRefrigeratoryId(@PathVariable(value = "id", required = true) @ApiParam(value = "冷库id", required = true) String RefrigeratoryId,
                                                  @PathVariable(value = "page", required = true) @ApiParam(value = "页数", required = true) Integer page,
                                                  @PathVariable(value = "size", required = true) @ApiParam(value = "行数", required = true) Integer size) {
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectPageInfoByRefrigeratoryId(RefrigeratoryId, page, size));
    }

    /**
     * 根据基地id查询冷库的入库信息
     * @param BaseId
     * @return
     */
    @ApiOperation(value = "根据基地id查询冷库的入库信息")
    @GetMapping("/selectInfoByBaseId/{id}")
    public Result selectInfoByBaseId(@PathVariable(value = "id", required = true) @ApiParam(value = "基地id", required = true) String BaseId) {
        List<Refrigeratory> refrigeratoryList = refrigeratoryService.getRefrigeratoryByBaseId(BaseId);
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectInfoByBaseId(refrigeratoryList));
    }

    /**
     * 根据基地id分页查询冷库的入库信息
     * @param BaseId
     * @return
     */
    @ApiOperation(value = "根据基地id分页查询冷库的入库信息")
    @GetMapping("/selectPageInfoByBaseId/{id}/{page}/{size}")
    public Result selectPageInfoByBaseId(@PathVariable(value = "id", required = true) @ApiParam(value = "基地id", required = true) String BaseId,
                                        @PathVariable(value = "page", required = true) @ApiParam(value = "页数", required = true) Integer page,
                                         @PathVariable(value = "size", required = true) @ApiParam(value = "行数", required = true) Integer size) {
        page = page <= 0 ? 1 : page;
        size = size <= 0 ? 1: size;
        List<Refrigeratory> refrigeratoryList = refrigeratoryService.getRefrigeratoryByBaseId(BaseId);
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectPageInfoByBaseId(refrigeratoryList, page, size));
    }

    /**
     * 查询所有入库信息（不限于基地和冷库）
     * @return
     */
    @ApiOperation(value = "查询所有入库信息（不限于基地和冷库）")
    @GetMapping("/selectAllInfo")
    public Result selectAllInfo() {
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectAllInfo());
    }

    /**
     * 分页查询所有入库信息（不限于基地和冷库）
     * @return
     */
    @ApiOperation(value = "分页查询所有入库信息（不限于基地和冷库）")
    @GetMapping("/selectAllInfo/{page}/{size}")
    public Result selectPageAllInfo(@PathVariable(value = "page") @ApiParam(value = "页数") Integer page,
                                    @PathVariable(value = "size") @ApiParam(value = "行数") Integer size) {
        page = page <= 0 ? 1 : page;
        size = size <= 0 ? 1: size;
        return Result.success("查询成功").data(refrigeratoryInInfoService.selectPageAllInfo(page, size));
    }

}
